/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elka.wdec.gui;

import com.elka.wdec.utils.Model;
import com.elka.wdec.utils.Rozwiazanie;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;

/**
 *
 * @author michal
 */
public class Plot extends Canvas
{
	public Plot()
	{
		model = null;
		ryzyko = 50;
	}
	@Override
	public void paint(Graphics g)
	{
		drawCoordinateSystem(g);
		drawPoints(g);
	}
	
	public void setModel(Model model)
	{
		this.model = model;
	}
	
	public void updateRyzyko(int ryzyko)
	{
		this.ryzyko = ryzyko;
	}
	
	/**
	 * Nanosi na wykres punkty
	 */
	private void drawPoints(Graphics g)
	{
		for (int i = 0; i <= 100; i++)
		{
			Rozwiazanie r = model.getNajlepszeRozwiazanie(i);
			if (r == null)
			{
				continue;
			}
			
			int x = (int) (r.getRyzyko() * (this.getSize().width - 50));
			int y = (int) (r.getZyskNorm() * (this.getSize().height - 50));
			
			if (i == ryzyko)
			{
				g.setColor(Color.RED);
				g.fillOval(x + 22, 372 - y, 6, 6);
			}
			else
			{
				g.setColor(Color.BLACK);
				g.drawOval(x + 22, 372 - y, 6, 6);
			}
			g.drawOval(x + 22, 372 - y, 6, 6);
		}
	}
	
	private void drawCoordinateSystem(Graphics g)
	{
		int w = this.getSize().width;
		int h = this.getSize().height;

		// Draws x axis
		g.drawLine(25, h-25, w-25, h-25);
		g.drawLine(w-25, h-25, w-30, h-30);
		g.drawLine(w-25, h-25, w-30, h-20);
		g.drawString("Ryzyko", w - 50, h - 10);
		
		//Draws y axis
		g.drawLine(25, h-25, 25, 25);
		g.drawLine(25, 25, 20, 30);
		g.drawLine(25, 25, 30, 30);
		g.drawString("Zysk", 10, 20);
	}
	
	private Model model;
	private int ryzyko;
}
